//
//  GuideController.m
//  Coach_HaiGeEr
//
//  Created by liuchen on 2018/9/12.
//  Copyright © 2018年 海格尔. All rights reserved.
//

#import "GuideController.h"
#import "LoginBySecretController.h"

@interface GuideController ()<UIScrollViewDelegate>
{
    UIScrollView *_guideScrollView;
    UIPageControl *_pageControl;
    CGFloat _width;
    CGFloat _height;
    CGFloat _ratio;
    NSInteger _numberOfPages;
}
@end

@implementation GuideController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createSubViews];
}

#pragma mark ==========创建视图==========
- (void)createSubViews{
    _numberOfPages = 4;
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    
    [self.view setBackgroundColor:[UIColor whiteColor]];
    _width = [UIScreen mainScreen].bounds.size.width;
    _height = [UIScreen mainScreen].bounds.size.height;
    _ratio = _height / 667;
    
    _guideScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, _width, _height)];

    _guideScrollView.contentSize = CGSizeMake(_width * _numberOfPages, _height);
    _guideScrollView.contentOffset = CGPointZero;
    _guideScrollView.delegate = self;
    //    _guideScrollView si
    _guideScrollView.pagingEnabled = YES;
    //    _guideScrollView.scrollEnabled = NO;
    _guideScrollView.bounces = NO;
    _guideScrollView.showsHorizontalScrollIndicator = NO;
    _guideScrollView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:_guideScrollView];
    _pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0,
                                                                  _height - 18 * _ratio,
                                                                  _width,
                                                                  18 * _ratio)];
    _pageControl.numberOfPages = _numberOfPages;
    _pageControl.currentPage = 0;
    
    for (NSInteger i=1; i<_numberOfPages+1; i++) {
        CGRect imageFrame = CGRectMake(_width * (i-1), 0, _width, _height);
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:imageFrame];
        [imageView setImage:[UIImage imageNamed:[NSString stringWithFormat:@"Guide%@", @(i)]]];
        [_guideScrollView addSubview:imageView];
    }
    
    CGRect nextFrame = CGRectMake(_width * (_numberOfPages-1)+(_width-200*_ratio)/2,
                                  _height-120* _ratio,
                                  200*_ratio,
                                  80*_ratio);
    UIButton *nextButton = [[UIButton alloc] initWithFrame:nextFrame];
    [nextButton setBackgroundColor:[UIColor clearColor]];
    [nextButton addTarget:self action:@selector(nextClicked) forControlEvents:UIControlEventTouchUpInside];
    [_guideScrollView addSubview:nextButton];
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"Coach_HaiGeEr"];
    [[NSUserDefaults standardUserDefaults]  synchronize];
}

- (void)nextClicked{
    [[UIApplication sharedApplication] keyWindow].rootViewController = [[UINavigationController alloc] initWithRootViewController:[[LoginBySecretController alloc] init]];
}

#pragma mark - UIScrollViewDelegate
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger page = scrollView.contentOffset.x/[UIScreen mainScreen].bounds.size.width;
    _pageControl.currentPage = page;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
